package com.ai.chatbackend.config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.memory.repository.jdbc.JdbcChatMemoryRepository;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ChatClientConfig {

    /**
     * 创建一个ChatClient
     * @param ollamaChatModel 模型
     * @return chatClient
     */
    @Bean
    public ChatClient chatClient(OllamaChatModel ollamaChatModel,ChatMemory chatMemory) {
        ChatClient chatClient = ChatClient.builder(ollamaChatModel)
                .defaultSystem("你是一个智能对话机器人，对话要简洁明了")
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build()).build();
        return chatClient;
    }

    /**
     * 创建一个ChatMemory 用于对话记忆
     * @param jdbcChatMemoryRepository 使用mysql存储
     * @return chatMemory
     */
    @Bean
    public ChatMemory chatMemory(JdbcChatMemoryRepository jdbcChatMemoryRepository){
        MessageWindowChatMemory build = MessageWindowChatMemory.builder()
                .maxMessages(20)
                .chatMemoryRepository(jdbcChatMemoryRepository)
                .build();
        return build;
    }
}
